#include <stdio.h>
#include <libavutil/log.h>
#include <libavformat/avformat.h>
#include <libavdevice/avdevice.h>

void open_file()
{
    AVFormatContext *fmt_ctx;
    int ret;
    char *url = "/Users/yuanxuzhen/study/mac/ffmpeg_demo/output/sync_714251_3900875.mp4";
    av_log(NULL, AV_LOG_ERROR, "open_file\n");
    ret = avformat_open_input(&fmt_ctx, url, NULL, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avformat_open_input %s", av_err2str(ret));
        goto __ERROR;
    }
    av_dump_format(fmt_ctx, 0, url, 0);
__ERROR:
    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
    }
}

void open_audio()
{
    av_log(NULL, AV_LOG_ERROR, "open_audio\n");
    AVFormatContext *fmt_ctx;
    int ret;
    char *url = ":0";
    AVInputFormat *input_format;
    avdevice_register_all(); //注册设备
    input_format = av_find_input_format("avfoundation");
    if (!input_format)
    {
        av_log(NULL, AV_LOG_ERROR, "av_find_input_format error\n");
        return;
    }

    ret = avformat_open_input(&fmt_ctx, url, input_format, NULL);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "avformat_open_input %s\n", av_err2str(ret));
        goto __ERROR;
    }
    av_dump_format(fmt_ctx, 0, url, 0);
__ERROR:
    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
    }
}

void open_video()
{
    av_log(NULL, AV_LOG_ERROR, "open_video\n");
    AVFormatContext *fmt_ctx;
    int ret;
    char *url = "0:";
    AVInputFormat *input_format;
    AVDictionary *options = NULL;
    avdevice_register_all(); //注册设备
    input_format = av_find_input_format("avfoundation");
    if (!input_format)
    {
        av_log(NULL, AV_LOG_ERROR, "open_video av_find_input_format error\n");
        return;
    }
    av_dict_set(&options, "video_size", "640x480", 0);
    av_dict_set(&options, "framerate", "30", 0);
    av_dict_set(&options, "pixel_format", "nv12", 0);
    ret = avformat_open_input(&fmt_ctx, url, input_format, &options);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open_video avformat_open_input %s\n", av_err2str(ret));
        goto __ERROR;
    }
    av_dump_format(fmt_ctx, 0, url, 0);
__ERROR:
    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
    }
}

void open_video_and_audio()
{
    av_log(NULL, AV_LOG_ERROR, "open_video_and_audio\n");
    AVFormatContext *fmt_ctx;
    int ret;
    char *url = "0:0";
    AVInputFormat *input_format;
    AVDictionary *options = NULL;
    avdevice_register_all(); //注册设备
    input_format = av_find_input_format("avfoundation");
    if (!input_format)
    {
        av_log(NULL, AV_LOG_ERROR, "open_video_and_audio av_find_input_format error\n");
        return;
    }
    av_dict_set(&options, "video_size", "640x480", 0);
    av_dict_set(&options, "framerate", "30", 0);
    av_dict_set(&options, "pixel_format", "nv12", 0);

    ret = avformat_open_input(&fmt_ctx, url, input_format, &options);
    if (ret < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "open_video_and_audio avformat_open_input %s\n", av_err2str(ret));
        goto __ERROR;
    }
    av_dump_format(fmt_ctx, 0, url, 0);
__ERROR:
    if (fmt_ctx)
    {
        avformat_close_input(&fmt_ctx);
    }
}

int main(int argc, char *argv[])
{
    av_log_set_level(AV_LOG_INFO);
    open_file();
    // open_audio();
    // open_video_and_audio();
    return 0;
}

